<?php

App::uses('AppController', 'Controller');

/**
 * CashDailies Controller
 *
 * @property CashDaily $CashDaily
 */
class CashDailiesController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->CashDaily->recursive = 1;
        $this->paginate = array('conditions' => array('CashDaily.state' => false),'order' => 'CashDaily.start_date DESC');
        $this->set('cashDailies', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->CashDaily->exists($id)) {
            throw new NotFoundException(__('Invalid cash daily'));
        }
        $options = array('conditions' => array('CashDaily.' . $this->CashDaily->primaryKey => $id));
        $this->set('cashDaily', $this->CashDaily->find('first', $options));
    }

    public function getLast($idCajero = null) {

        if (isset($idCajero)) {
            $conditions = array('CashDaily.state' => false, 'CashDaily.user_id' => $idCajero);
        } else {
            $conditions = array('CashDaily.state' => false);
        }

        $options = array('conditions' => $conditions, 'order' => array('CashDaily.id DESC'));
        $cashDaily = $this->CashDaily->find('first', $options);

        return $cashDaily['CashDaily']['id'];
    }

    public function getCash($id = null) {
        $options = array('conditions' => array('CashDaily.user_id' => $id, 
            'CashDaily.state' => false
            ));
        $this->set('cashDaily', $this->CashDaily->find('first', $options));
        $this->layout = 'ajax';
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->CashDaily->create();
            if ($this->CashDaily->save($this->request->data)) {
                $this->Session->setFlash(__('Caja Inicializa Correctamente'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('No se inicializo la caja.'));
            }
        }
        $users = $this->CashDaily->User->find('list');
        $this->set(compact('users'));
    }

    public function init() {
        if ($this->request->is('post')) {
            $this->CashDaily->create();
            if ($this->CashDaily->save($this->request->data)) {
                $this->Session->setFlash(__('Caja Inicializa Correctamente'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('No se inicializo la caja'));
            }
        }
        $users = $this->CashDaily->User->find('list', array('conditions' => array('User.role' => 'cajero')));
        $this->set(compact('users'));
    }

    public function close($id = null) {
        $this->CashDaily->id = $id;
        if ($this->CashDaily->saveField('state', 1) && $this->CashDaily->saveField('end_date', date('Y-m-d H:i:s'))) {
            $this->Session->setFlash(__('La caja fue cerrada correctamente'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('La caja NO fue cerrada. Intente nuevamente'));
        }
        $users = $this->CashDaily->User->find('list');
        $this->set(compact('users'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->CashDaily->exists($id)) {
            throw new NotFoundException(__('Invalid cash daily'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->CashDaily->save($this->request->data)) {
                $this->Session->setFlash(__('The cash daily has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The cash daily could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('CashDaily.' . $this->CashDaily->primaryKey => $id));
            $this->request->data = $this->CashDaily->find('first', $options);
        }
        $users = $this->CashDaily->User->find('list');
        $this->set(compact('users'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->CashDaily->id = $id;
        if (!$this->CashDaily->exists()) {
            throw new NotFoundException(__('Invalid cash daily'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->CashDaily->delete()) {
            $this->Session->setFlash(__('La caja fue borrada correctamente'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('La caja fue NO fue borrada'));
        $this->redirect(array('action' => 'index'));
    }

}
